/**
***********************************************************************
*
* Copyright 2011 Stefan Wöhle
*
* This file is part of PixelMath
*
* PixelMath is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PixelMath is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PixelMath. If not, see <http://www.gnu.org/licenses/>.
*
***********************************************************************
*/

#include "emptyformulawidget.h"
#include <QAction>
#include "xwidget.h"
#include "ywidget.h"
#include "constantwidget.h"
#include "sinewidget.h"
#include "localizedfunctionwidget.h"
#include "rvalue.h"
#include "gvalue.h"
#include "bvalue.h"
#include "operatorwidget.h"
#include "addition.h"
#include "subtraction.h"
#include "multiplication.h"
#include "division.h"

EmptyFormulaWidget::EmptyFormulaWidget(QWidget *parent) :
    FormulaWidget(parent),
    button(tr("Empty"), this)
{
    /* Appearance
     *****************************
     *  QPushButton with QMenu v *
     *****************************/
    this->centralLayout.addWidget(&button);
    button.setMenu(&contextMenu);
    contextMenu.clear();

    QAction *action;
    action = contextMenu.addAction(tr("X"));
    connect(action, SIGNAL(triggered()),
            this, SLOT(xTriggered()));
    action = contextMenu.addAction(tr("Y"));
    connect(action, SIGNAL(triggered()),
            this, SLOT(yTriggered()));
    action = contextMenu.addAction(tr("Constant"));
    connect(action, SIGNAL(triggered()),
            this, SLOT(constantTriggered()));
    action = contextMenu.addAction(tr("Sine"));
    connect(action, SIGNAL(triggered()),
            this, SLOT(sineTriggered()));

    QMenu *subMenuLocalized = contextMenu.addMenu(tr("Localized Functions"));
    action = subMenuLocalized->addAction(tr("R-Value"));
    connect(action, SIGNAL(triggered()),
            this, SLOT(rValueTriggered()));
    action = subMenuLocalized->addAction(tr("G-Value"));
    connect(action, SIGNAL(triggered()),
            this, SLOT(gValueTriggered()));
    action = subMenuLocalized->addAction(tr("B-Value"));
    connect(action, SIGNAL(triggered()),
            this, SLOT(bValueTriggered()));

    QMenu *subMenuOperator = contextMenu.addMenu(tr("Operation"));
    action = subMenuOperator->addAction(tr("Addition"));
    connect(action, SIGNAL(triggered()),
            this, SLOT(additionTriggered()));
    action = subMenuOperator->addAction(tr("Subtraction"));
    connect(action, SIGNAL(triggered()),
            this, SLOT(subtractionTriggered()));
    action = subMenuOperator->addAction(tr("Multiplication"));
    connect(action, SIGNAL(triggered()),
            this, SLOT(multiplicationTriggered()));
    action = subMenuOperator->addAction(tr("Division"));
    connect(action, SIGNAL(triggered()),
            this, SLOT(divisionTriggered()));
}

/*********************************************************
 *
 *  ...Triggered
 *
 *  Corresponding menu entry was triggered. Create choosen
 *  widget and distribute it via the formulaChoosen-signal.
 *
 **********************************************************/

void EmptyFormulaWidget::xTriggered()
{
    emit formulaChoosen(new XWidget(parentWidget()));
}

void EmptyFormulaWidget::yTriggered()
{
    emit formulaChoosen(new YWidget(parentWidget()));
}

void EmptyFormulaWidget::constantTriggered()
{
    emit formulaChoosen(new ConstantWidget(parentWidget()));
}

void EmptyFormulaWidget::sineTriggered()
{
    emit formulaChoosen(new SineWidget(parentWidget()));
}

void EmptyFormulaWidget::rValueTriggered()
{
    emit formulaChoosen(new LocalizedFunctionWidget(new RValue(parentWidget()), parentWidget()));
}

void EmptyFormulaWidget::gValueTriggered()
{
    emit formulaChoosen(new LocalizedFunctionWidget(new GValue(parentWidget()), parentWidget()));
}

void EmptyFormulaWidget::bValueTriggered()
{
    emit formulaChoosen(new LocalizedFunctionWidget(new BValue(parentWidget()), parentWidget()));
}

void EmptyFormulaWidget::additionTriggered()
{
    emit formulaChoosen(new OperatorWidget(new Addition(parentWidget()), parentWidget()));
}

void EmptyFormulaWidget::subtractionTriggered()
{
    emit formulaChoosen(new OperatorWidget(new Subtraction(parentWidget()), parentWidget()));
}

void EmptyFormulaWidget::multiplicationTriggered()
{
    emit formulaChoosen(new OperatorWidget(new Multiplication(parentWidget()), parentWidget()));
}

void EmptyFormulaWidget::divisionTriggered()
{
    emit formulaChoosen(new OperatorWidget(new Division(parentWidget()), parentWidget()));
}
